package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
/**
 * http://www.hualongxiang.com/ 化龙巷论坛
 * @author abc
 */
public class HualongxiangBbsAnalyse implements AnalyseNews{

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://www.hualongxiang.com/[a-z]*/[0-9]{5,}",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta bbs = new NewsMeta();
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		bbs.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		String author = null;
		Document doc = Jsoup.parse(htmltxt);
		doc.select("a:containsOwn(登录/注册后可看大图)").remove();
		Element first = doc.select("div#floot_left span.fl.b").first();
		if(first!=null){
			author = first.text();
		}
			title =doc.select("h1#subject_tpc.read_h1").text();
			if(StringUtils.isBlank(title)){
				title =doc.select("div.title_name h2").text();
			}
			content = HtmlCleaner.getContentHtml(url, doc.select("div#read_tpc"));
			String tempdate=doc.select("td#td_tpc.floot_right h1 span.fr").text();
			if(StringUtils.isBlank(tempdate)){
				tempdate=doc.select("div.cont_hd  p.color_666").text();
			}
			date = DateUtils.matchDate(tempdate);

			bbs.setTitle(StringUtils.trimSpace(title));
			bbs.setContent(content);
			bbs.setDate(date);

			if(StringUtils.isBlank(title)||date==null){
				if(doc.select("div.tip_notices:contains(您没有登录或者您没有权限访问此页面，可能有如下几个原因：)").size()>0){
					bbs.setUpdateUrl(ParseState.FORBID.toString());
					return bbs;
				}
			}
			bbs.setAuthor(author);
			/**
			 * 解析用于更新的地址
			 */
			bbs.setUpdateUrl(url);

		return bbs;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
    
	
}
